package com.wxg.config.rocketmq;

import com.wxg.config.MqProperties;
import com.wxg.domain.MqConstant;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.consumer.AllocateMessageQueueStrategy;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.MQConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * @author void
 * @date 2019/12/11 15:20
 * @desc rocketMq 消费者
 */
@Slf4j
@Configuration
@Conditional(value = RocketCondition.class)
public class RocketMqConsumer {

    @Autowired
    private MqProperties mqProperties;
    @Autowired
    private AllocateMessageQueueStrategy iRocketMqStrategy;

    @Bean
    public MQConsumer consumer1(){
        MQConsumer consumer = getConsumer(MqConstant.Group.GID_VOID_1, new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
                log.info("consumer1消费 线程名:{},收到新消息:{},消息内容:{}", Thread.currentThread().getName(), msgs, new String(msgs.get(0).getBody()));
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
        return consumer;
    }

    @Bean
    public MQConsumer consumer2(){
        MQConsumer consumer = getConsumer2(MqConstant.Group.GID_VOID_2, new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
                log.info("consumer2消费 线程名:{},收到新消息:{},消息内容:{}", Thread.currentThread().getName(), msgs, new String(msgs.get(0).getBody()));
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
        return consumer;
    }

    public MQConsumer getConsumer(MqConstant.Group group, MessageListenerConcurrently messageListener){
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(group.name());
        consumer.setNamesrvAddr(mqProperties.getNameSrvAddr());
        try {
            consumer.subscribe(group.getTopic().name(), group.getTag().name());
            consumer.registerMessageListener(messageListener);
            consumer.start();
        } catch (MQClientException e) {
            e.printStackTrace();
        }
        System.out.printf("RocketMq Consumer:{} Started", group.name());
        return consumer;
    }

    public MQConsumer getConsumer2(MqConstant.Group group, MessageListenerConcurrently messageListener){
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(group.name(), null, iRocketMqStrategy);
        consumer.setNamesrvAddr(mqProperties.getNameSrvAddr());
        try {
            consumer.subscribe(group.getTopic().name(), group.getTag().name());
            consumer.registerMessageListener(messageListener);
            consumer.start();
        } catch (MQClientException e) {
            e.printStackTrace();
        }
        System.out.printf("RocketMq Consumer:{} Started", group.name());
        return consumer;
    }
}
